﻿using MyLogPC.SQLs;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyLogPC
{
    public partial class AlarmSearchFrm : Form
    {


        public AlarmSearchFrm()
        {
            InitializeComponent();
        }

        private void cb_NowTime_CheckedChanged(object sender, EventArgs e)
        {
            if (cb_NowTime.Checked)
            {
                dtp_EndTime.Enabled = false;
                dtp_EndTime.Value = DateTime.Now;
            }
            else
            {
                dtp_EndTime.Enabled = true;
            }
        }

        private async void btn_Search_Click(object sender, EventArgs e)
        {
            //执行查询操作，暂时只使用时间，其他参数忽略
            DateTime startTime = dtp_StartTime.Value;
            DateTime endTime = cb_NowTime.Checked ? DateTime.Now : dtp_EndTime.Value;
            if (startTime > endTime)
            {
                MessageBox.Show("开始时间不能大于结束时间", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            var alarmData = await DbHelper.QueryAlarmsAsync(startTime, endTime);
            if (alarmData == null || alarmData.Count == 0)
            {
                MessageBox.Show("没有查询到相关数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //将查询结果绑定到DataGridView
            dgv_AlarmData.DataSource = alarmData;

        }

        private void dgv_AlarmData_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            //设置DataGridView的列宽自适应,指定列名为samplingTime的列显示在第一列
            dgv_AlarmData.Columns["AlarmId"].DisplayIndex = 0;
            dgv_AlarmData.Columns["SamplingTime"].DisplayIndex = 1;
            dgv_AlarmData.Columns["SamplingTime"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss.fff";
            dgv_AlarmData.Columns["DescriptionCN"].DisplayIndex = 2;
            //隐藏description列
            if (dgv_AlarmData.Columns.Contains("Description")) dgv_AlarmData.Columns["Description"].Visible = false;
            
            foreach (DataGridViewColumn column in dgv_AlarmData.Columns)
            {
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; //设置列宽自适应
            }



        }
    }
}
